﻿Public Class ForgetPassword
    Inherits System.Web.UI.Page

    Public dbmanager = New DBManager

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnReset.Click
        Dim KeyGen As RandomKeyGenerator
        Dim passwordKey As String
        KeyGen = New RandomKeyGenerator
        KeyGen.KeyLetters = "abcdefghijklmnopqrstuvwxyz"
        KeyGen.KeyNumbers = "0123456789"
        KeyGen.KeyChars = 10
        passwordKey = KeyGen.Generate()

        Dim hash = dbmanager.hashPassword(passwordKey)
        If (dbmanager.forgetPassword(tbxEmail.Text, hash)) Then
            sendEmail(passwordKey)
            MsgBox("A new password is sent to your email")
        Else
            MsgBox("The email address entered is incorrect")
        End If
    End Sub
    Protected Sub sendEmail(ByVal passwordKey As String)
        Try

            Dim s As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("smtp.gmail.com", 587)
            Dim nc As System.Net.NetworkCredential = New System.Net.NetworkCredential("goodanot@gmail.com", "threepotatoes")
            s.EnableSsl = True
            s.UseDefaultCredentials = False
            s.Credentials = nc

            s.Send("goodanot@gmail.com", tbxEmail.Text, "Forgot Password", "Your new password is " + passwordKey + "." + "Please click on the link : " + "http://localhost:57166/Home.aspx" + " to login")
        Catch ex As Exception
        End Try
    End Sub
End Class